package org.credo.net.study;

import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.DefaultHttpClient;

/**
 * <p>Description: Http的各种可共用的方法.</p>
 * @author <a href="zhaoqianjava@qq.com">Credo</a>
 */
public class HttpUtils {

	public static void main(String[] args) {

	}

	/**
	 * 发送一个Http Get请求.
	 * @param url
	 * @return httpStatusCode,200成功
	 */
	public static int sendHttpGetRequest(String url) {
		HttpClient httpClient = new DefaultHttpClient();
		int httpStatusCode = 400;
		try {
			HttpGet httpGet = new HttpGet(url);
			HttpResponse response = httpClient.execute(httpGet);
			httpStatusCode = response.getStatusLine().getStatusCode();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			httpClient.getConnectionManager().shutdown();
		}
		return httpStatusCode;
	}
	
	/**
	 * @url rest服务的URL链接
	 * @bayteArray 转换好的要POST传输的数据
	 * @params 预留的参数MAP集
	 * @return 正确发送并响应正确信息返回true,否则false
	 */
	public static boolean sendHttpPostRequest(String url, byte[] bayteArray, Map<String, String> params) {
		HttpClient httpClient = new DefaultHttpClient();
		int httpStatusCode = 400;
		//boolean httpResult = false;
		try {
			HttpPost httpPost = new HttpPost(url);
			httpPost.setEntity(new ByteArrayEntity(bayteArray));

			// 一个名称/值对参数用作一个元素的HTTP消息。保留以防需求变更
			// List<NameValuePair> nvps = new ArrayList<NameValuePair>();
			// if (null != params) {
			// Set<String> keySet = params.keySet();
			// for (String key : keySet) {
			// nvps.add(new BasicNameValuePair(key, params.get(key)));
			// }
			// }
			// httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));

			HttpResponse response = httpClient.execute(httpPost);
			httpStatusCode = response.getStatusLine().getStatusCode();
			if (httpStatusCode != 200) {
				return false;
			} else {
				return true;
			}
			// System.out.println("return的httpStatusCode为:" + httpStatusCode);

			// HttpEntity httpEntity = response.getEntity();
			// if (httpEntity.isStreaming()) {
			// InputStream inputStream = httpEntity.getContent();
			// Result result = (Result) MarshalUtil.unmarshal(inputStream,
			// Result.class);
			// if (result.getSuccess().toString().equalsIgnoreCase("SUCCESS") ||
			// result.getSuccess().toString().equalsIgnoreCase("SUCCESSINFO")) {
			// httpResult = true;
			// }
			// System.out.println("POST请求响应的XML文件Result的SUCCESS属性为:" +
			// result.getSuccess().toString());
			// }
			// if (httpStatusCode == 200 && httpResult) {
			// httpResult = true;
			// } else {
			// httpResult = false;
			// }

		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			httpClient.getConnectionManager().shutdown();
		}
		// return httpResult;
	}
}
